<template>
  <el-dialog
    width="400px"
    title="选择应用"
    :visible.sync="params.visible"
  >
    <compTree
      ref="tree"
      :tree-show-checkbox="treeShowCheckbox"
      :comp-tree-height="400"
      :tree-data="treeData"
      :highlight-current="true"
      :show-search="false"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
      :tree-node-select="treeNodeSelect"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>
<script>
/**
 * 入参(可选)：
 *    params: {
 *        programName,
 *        isNullSelect,
 *        computerName,
 *        programID,
 *        isMutil,
 *        computerID
 *     }
 */
import { getProgramList, getWebTargetGroup } from '@/api/core'
import { ParamArr, SQLTable } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
export default {
  name: 'SelectProgram',
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          programID: '',
          isNullSelect: false,
          isMutil: false,
          computerID: '',
          programName: '',
          computerName: ''
        }
      }
    }
  },
  data() {
    return {
      checked: false,
      formData: '',
      treeShowCheckbox: false,
      treeData: [],
      nodeRob: {},
      nodeCheck: [],
      result: {},
      cmtProgram: 3,
      treeDefaultCheckedKeys: [],
      node: {}, // 当前选中的节点
      parentNode: {} // 当前选中节点的父节点
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.formData = this.params
      this.treeShowCheckbox = this.formData.isMutil
      const vPar = new ParamArr()
      if (this.formData.programName !== undefined && this.formData.computerName !== undefined) {
        vPar.setStr('IPs', this.formData.computerName)
        getProgramList(vPar).then(vPar => {
          const nodes = []
          const nodeMap = new Map()
          const table = new SQLTable(vPar.getTable('k_table'))
          table.first()
          while (!table.eof()) {
            const pnode = Global.getTreeviewPNode(nodeMap, nodes, '')
            const node = {}
            node['id'] = Global.getGuid()
            node['name'] = table.getValueByName('Name')
            node['text'] = node['name']
            node['type'] = 3
            node['icon'] = 'yingyong2'
            pnode.push(node)
            table.next()
          }
          this.treeData = nodes
          // 数据回显
          for (let i = 0; i < nodes.length; i++) {
            if (nodes[i].type === 3 && Global.strFormat('{0}{1}{2}', ',', this.formData.programName, ',').indexOf(Global.strFormat('{0}{1}{2}', ',', nodes[i].name, ',')) !== -1) {
              this.treeDefaultCheckedKeys.push(nodes[i].id)
            }
          }
        })
      } else if (this.formData.computerID !== undefined) {
        vPar.setStr('ComputerID', this.formData.computerID)
        getWebTargetGroup(vPar).then(vPar => {
          const nodes = []
          const nodeMap = new Map()
          let table = new SQLTable(vPar.getTable('k_group'))
          table.first()
          while (!table.eof()) {
            const pnode = Global.getTreeviewPNode(nodeMap, nodes, table.getValueByName('PID'))
            const node = {}
            node['id'] = table.getValueByName('ID')
            node['name'] = table.getValueByName('Name')
            node['text'] = node['name']
            node['type'] = 1
            node['icon'] = 'wenjianyi'
            pnode.push(node)
            nodeMap.set(table.getValueByName('ID'), node)
            table.next()
          }
          table = new SQLTable(vPar.getTable('k_computer'))
          table.first()
          while (!table.eof()) {
            const pnode = Global.getTreeviewPNode(nodeMap, nodes, table.getValueByName('GroupID'))
            const node = {}
            node['id'] = table.getValueByName('ID')
            node['name'] = table.getValueByName('IP')
            node['text'] = node['name']
            node['icon'] = Global.getSystemIconStr(table.getValueByName('System').trim())
            node['tags'] = [table.getValueByName('System')]
            pnode.push(node)
            nodeMap.set(table.getValueByName('ID'), node)
            table.next()
          }
          table = new SQLTable(vPar.getTable('k_program'))
          table.first()
          while (!table.eof()) {
            const pnode = Global.getTreeviewPNode(nodeMap, nodes, table.getValueByName('ComputerID'))
            const node = {}
            node['id'] = table.getValueByName('ID')
            if (node['id'] === this.formData.programID) this.treeDefaultCheckedKeys.push(node['id'])
            node['name'] = table.getValueByName('Name')
            node['text'] = node['name']
            node['type'] = 3
            node['icon'] = 'yingyong2'
            pnode.push(node)
            table.next()
          }
          this.treeData = nodes
          // 数据回显
          if (this.formData.isMutil !== undefined && this.formData.isMutil) {
            for (let i = 0; i < nodes.length; i++) {
              if (nodes[i].type === 3 && Global.strFormat('{0}{1}{2}', ',', this.formData.programID, ',').indexOf(Global.strFormat('{0}{1}{2}', ',', nodes[i].id, ',')) !== -1) {
                this.treeDefaultCheckedKeys.push(nodes[i].id)
              }
            }
          }
        })
      }
    },
    getReturn2() {
      const nodeCheck = this.$refs.tree.getCheckedNodes()
      var arrName = []
      for (var i = 0; i < nodeCheck.length; i++) {
        var node = nodeCheck[i]
        switch (node.type) {
          case 3:
            arrName.push(node.name)
            break
        }
      }
      var obj = {}
      obj.arrName = arrName
      return obj
    },
    getReturn() {
      const nodeCheck = this.$refs.tree.getCheckedNodes()
      var arrID = []
      var arrName = []
      for (var i = 0; i < nodeCheck.length; i++) {
        var node = nodeCheck[i]
        switch (node.type) {
          case 3:
            if (node.id !== '') {
              arrID.push(node.id)
              arrName.push(node.name)
            }
            break
        }
      }
      var obj = {}
      obj.arrID = arrID
      obj.arrName = arrName
      return obj
    },
    treeNodeSelect(node, pnode) {
      this.node = node
      if (pnode.parent !== undefined) {
        this.parentNode = pnode.parent.data
      }
    },
    confirm() {
      const nodes = this.$refs.tree.getCheckedNodes()
      if (this.formData.computerName !== undefined) {
        if (nodes.length === 0) return this.$message.warning('请选择程序！')
        this.result.programName = this.getReturn2().arrName.toString()
      } else {
        if (this.formData.isMutil) {
          if (nodes.length === 0) return this.$message.warning('请选择程序！')
          this.result.programID = this.getReturn().arrID.toString()
          this.result.programName = this.getReturn().arrName.toString()
        } else {
          if (this.node.id === undefined) return this.$message.warning('请选择程序！')
          this.result.programID = this.node['id']
          this.result.programName = Global.strFormat('[{0}][{1}]', this.parentNode.name, this.node.name)
          this.cID = this.parentNode['id']
          this.cIp = this.parentNode.name
        }
      }
      if (this.params.confirm !== undefined) {
        this.params.confirm(this.result)
      } else {
        this.$emit('confirm', this.result)
      }
    }
  }
}
</script>
